//
//  AdaptCtr.m
//  tableViewDemo
//
//  Created by lix on 16/8/25.
//  Copyright © 2016年 lix. All rights reserved.
//

#import "AdaptCtr.h"
#import "AdaptSecondCtr.h"
#import "ModelAdapt.h"
#import "DmTVC.h"

@interface AdaptCtr ()

@property(nonatomic,strong) NSMutableArray * dataArr;
@property(nonatomic,strong) NSMutableArray * heightArr;
@property(nonatomic,strong) ModelAdapt * manager;

@end

@implementation AdaptCtr

- (id)init{
    if (self = [super init]) {
        _dataArr = [[NSMutableArray alloc]init];
        _heightArr = [[NSMutableArray alloc]init];
        _manager = [ModelAdapt sharedCoreDataManager];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"tableViewCell自适应";
    
    self.view.backgroundColor = [UIColor whiteColor];
    //取消随navigation的位置自动调整
    self.automaticallyAdjustsScrollViewInsets = NO;

    //定义添加按钮
    [self initNavigate];
    
    [self initTableVIew];
}
//"添加"按钮
- (void)initNavigate{
    UIBarButtonItem * rightItem  = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(pushView:)];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)pushView:(id)but{
    
    AdaptSecondCtr * asvc = [[AdaptSecondCtr alloc]init];
    
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    [self.navigationController pushViewController:asvc animated:YES];
}

- (void)initTableVIew{
    _tableView = [[UITableView alloc]initWithFrame:FRAME style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    //? 网上自适应解决代码，不需要计算，没用明白
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 200;
    

    [self.view addSubview:_tableView];
    
}
//左滑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ModelAdapt * dataObj = [_dataArr objectAtIndex:indexPath.row];
    
    [_manager delData:dataObj.adapt_id];
    
    [self TableViewReloadData];
}
//cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DMTVC * cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    
    return cell.cellHeight+10;
}

- (CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 64;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * CellIdentifier = @"cell";
    DMTVC * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == NULL) {
        cell = [[DMTVC alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    
    ModelAdapt * dataObj = [_dataArr objectAtIndex:indexPath.row];
    [cell setTextForLabel:dataObj.adapt_content];
    CGFloat cellHeight = [cell cellHeight];
    
    [_heightArr addObject:[NSString stringWithFormat:@"%f",cellHeight]];
    
    return cell;
}
- (void)viewWillAppear:(BOOL)animated{
    
    NSLog(@"appear");
    
    [self TableViewReloadData];
    
    [super viewWillAppear:animated];
}

- (void)TableViewReloadData{
    NSArray * fetchResult = [_manager selectAll];
        
    [self.dataArr removeAllObjects];
    [self.dataArr addObjectsFromArray:fetchResult];
    [self.tableView reloadData];
        
}



@end
